package com.qf.Servlet;

import com.qf.Service.PatientInfoService;
import com.qf.Service.impl.PatientInfoServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.PatientInfo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "PatientInfoServlet", urlPatterns = "/PatientInfoServlet")
public class PatientInfoServlet extends BaseServlet {
    private PatientInfoService patientInfoService = new PatientInfoServiceImpl();

    public void addPatientInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接受前端传递的参数
        String patientIdcard = request.getParameter("patientIdcard");
        String patientName = request.getParameter("patientName");
        String patientMoney = request.getParameter("patientMoney");

        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setPatientIdcard(Long.parseLong(patientIdcard));
        patientInfo.setPatientName(patientName);
        patientInfo.setPatientMoney(patientMoney);

        int num = patientInfoService.addPatientInfo(patientInfo);

        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();

        if (num < 0) {
            resultVo.setCode(200);
            resultVo.setMessage("成功");
            resultVo.setData(true);
        }else {
            resultVo.setCode(500);
            resultVo.setMessage("失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);

    }

    public void deletePatientInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");

        PrintWriter pw = response.getWriter();

        ResultVo<Boolean> resultVo = new ResultVo<>();
        int num = patientInfoService.deletePatientInfo(name);
        if (num != -1) {
            resultVo.setCode(200);
            resultVo.setMessage("成功");
            resultVo.setData(true);
        } else {
            resultVo.setCode(500);
            resultVo.setMessage("失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void selectPatientInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        List<PatientInfo> patientInfoList = patientInfoService.selectPatientInfo(name);

        PrintWriter pw = response.getWriter();

        ResultVo<List<PatientInfo>> resultVo = new ResultVo<>();
        if (patientInfoList != null && patientInfoList.size() > 0) {
            resultVo.setData(patientInfoList);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        }else {
            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void updatePatientInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String patientIdcard = request.getParameter("patientIdcard");
        String patientName = request.getParameter("patientName");
        String patientMoney = request.getParameter("patientMoney");

        PatientInfo patientInfo = new PatientInfo();
        patientInfo.setPatientIdcard(Long.parseLong(patientIdcard));
        patientInfo.setPatientName(patientName);
        patientInfo.setPatientMoney(patientMoney);

        int num = patientInfoService.updatePatientInfo(patientInfo);
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num != -1) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw, resultVo);
    }

}
